/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"48AC898B-CF17-49C0-8A99-3236BF63DABC",variableType:93}
 */
var fFechaMov = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"599B092E-8CAC-4F6D-8CA3-E179CF474BB9",variableType:4}
 */
var fAnio = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"044CE8CD-1516-46FB-9E7C-E8D7AA7E5341",variableType:4}
 */
var fMes = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"5F3CC9C3-B37B-4185-BDA4-0E40DA869EDB",variableType:4}
 */
var fReciboNro = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"5B6A844A-1734-4286-BFC0-5E77982B1B08",variableType:8}
 */
var fRecargo = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"24AD5FE5-7AE9-4004-9F28-3FBA527D7385",variableType:8}
 */
var fTotalC = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"B4A966EE-C18F-49F6-9F34-74E7A11C9BB1",variableType:8}
 */
var fTotalB = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"F485A3D3-89B4-4782-B8BE-45DB4F188012",variableType:93}
 */
var fVto2 = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"F44905AA-9F65-4CFD-B8E9-8626B78FE2C3",variableType:93}
 */
var fVto1 = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"BC1B4808-18FF-4CC8-9873-DF2365CEBF3B",variableType:8}
 */
var fTotalA = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D99C5474-A1E8-4AA9-A8E6-593DC977BA07"}
 */
function onShow(firstShow, event) {
	var $hoy 	= application.getServerTimeStamp()
	fVto1 		= new Date($hoy.getFullYear(), $hoy.getMonth()+1, 10, 0, 0, 0, 0)
	fVto2 		= new Date($hoy.getFullYear(), $hoy.getMonth()+1, 20, 0, 0, 0, 0)
	fRecargo 	= 5
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3E83C409-35FA-4EB3-9A25-06035DF8E654"}
 */
function comenzar(event) {
	
	databaseManager.setAutoSave(false)
	var $sql = 'SELECT u.unidad_id, unidad_funcional, coef_a, coef_b, coef_c, (select SUM(importe_signo) from cta_cte "cc" where cc.unidad_id = u.unidad_id) as "deuda" '+
				'FROM unidades "u" WHERE u.consorcio_id = ?'
	var $args = [globals.gConsorcio]
	var $ds = databaseManager.getDataSetByQuery('consorcios',$sql,$args,-1)
	
	var $exp = databaseManager.getFoundSet('consorcios','liquidaciones')
	for (var i = 1 ; i <= $ds.getMaxRowIndex() ; i++)
	{	
		$exp.newRecord()
		$exp.anio					= fAnio
		$exp.mes					= fMes
		$exp.consorcio_id           = globals.gConsorcio
		$exp.importe_exp_a          = fTotalA * $ds.getValue(i,3) / 100 
		$exp.importe_exp_b          = fTotalB * $ds.getValue(i,4) / 100
		$exp.importe_exp_c          = fTotalC * $ds.getValue(i,5) / 100
		if ($ds.getValue(i,6) == 0)
			$exp.deuda = 0
		else
			$exp.deuda              = $ds.getValue(i,6)
		$exp.fecha_vencimiento_1    = fVto1
		$exp.fecha_vencimiento_2    = fVto2
		
		var $total	 				= $exp.importe_exp_a + $exp.importe_exp_b + $exp.importe_exp_c + $exp.deuda
		$exp.total_vencimiento_1    = Math.round($total)
		$exp.porcentaje_recargo     = fRecargo
		$exp.interes                = $exp.total_vencimiento_1 * fRecargo / 100 
		$exp.total_vencimiento_2    = Math.round($exp.total_vencimiento_1 + $exp.interes)
		$exp.unidad_funcional       = $ds.getValue(i,2)
		$exp.recibo_nro				= fReciboNro + i - 1
		
		var $unidad			= $ds.getValue(i,1) 
		var $cod_mov 		= 2
		var $concepto 		= 'Expensas del Mes '+utils.numberFormat(fVto1.getMonth() + 1,'00')
		var $importe 		= $exp.importe_exp_a + $exp.importe_exp_b + $exp.importe_exp_c
		var $fecha 			= fFechaMov
		globals.gAltaCtacte($unidad,$cod_mov,$concepto,Math.round($importe),$fecha)
	}
	
	if (databaseManager.saveData())
	{
		databaseManager.setAutoSave(true)
		globals.DIALOGS.showInfoDialog('Aviso','Proceso terminado con éxito', 'OK')
	}
	else
	{
		databaseManager.revertEditedRecords()
		databaseManager.setAutoSave(true)
		globals.DIALOGS.showInfoDialog('Error','Ocurrió un error en el proceso', 'OK')
	}
	forms.liquidaciones_tbl.controller.show()
}
